/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

MapdataOutputer - 类名称
修改许可权限 - 修改人名称

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function MapdataOutputer()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.map.dataOutput{
	import com.OpenFile;
	
	import editor.common.Common;
	import editor.manager.MapManger;
	import editor.map.TileLayer;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	import flash.utils.ByteArray;
	
	import mx.controls.Alert;
	import mx.graphics.codec.PNGEncoder;
	
	public class MapdataOutputer{
		
		public static var png:ByteArray = null;
		public static var mapData:String = null;
		
		/**
		 * 用bitmap的形式输出tile节点信息
		 * @param dataVector 存储节点的二维数组 
		 */		
		public static function outputTileBitmapdata(dataVector:Array):void
		{
			var dataXml:String = outPutXmlFile();
			if(dataXml == null) return ;
			var width:int = TileLayer.MAP_LOGIC_WIDTH;
			var height:int = TileLayer.MAP_LOGIC_HEIGHT;
			var bmp:BitmapData = new BitmapData(width, height);
			for(var i:int = 0; i < width; i ++)
			{
				for(var j:int = 0; j < height; j ++)
				{
					if(dataVector[i][j])
						bmp.setPixel(i,j,0xFFFFFF);
					else
						bmp.setPixel(i,j,0x000000);
				}
			}
			var encoder:PNGEncoder = new PNGEncoder();
			png = encoder.encode(bmp);
			mapData = dataXml;
			
			var file:File = new File();
			file.browseForDirectory("寻找文件保存路径");
			file.addEventListener(Event.SELECT, onSave);
			//			var file:File = File.desktopDirectory.resolvePath("ok" + Math.random() + ".png");
			//			var fileStream:FileStream = new FileStream();
			//			fileStream.open(file, FileMode.WRITE);
			//			fileStream.writeBytes();
			//			fileStream.close();

			//			var file:FileReference=  new FileReference();
			//			file.save(encoder.encode(bmp), Common.map.gmap.mapName + ".png");
			//			var fileXml:FileReference = new FileReference();
			//			fileXml.save(dataXml, Common.map.gmap.mapName + ".xml");
		}
		
		private static function onSave(evt:Event):void
		{
			var file:File = evt.target as File;
			var fileStream:FileStream = new FileStream();
			var pngFile:File = file.resolvePath(Common.map.gmap.mapId + ".png");
			fileStream.open(pngFile, FileMode.WRITE);
			fileStream.writeBytes(png);
			fileStream.close();
			
			var xmlFile:File = file.resolvePath(Common.map.gmap.mapId + ".xml");
			fileStream.open(xmlFile, FileMode.WRITE);
			
			fileStream.writeUTFBytes(mapData);
			fileStream.close();
			trace(mapData)
		}
		
		/**
		 * 输出地图的xml文件 
		 */		
		public static function outPutXmlFile():String
		{
			var npcList:Vector.<Npc> = Common.map.npcLayer.npcList;
			var mapName:String = Common.map.gmap.mapName;
			var mapId:String = Common.map.gmap.mapId;
			
			var inGate:Teleport = Common.map.npcLayer.inGate;
			var outGate:Teleport = Common.map.npcLayer.outGate;
			if(inGate == null || outGate == null)
				Alert.show("请设定好进出城位置");
			else
			{
				var sceneData:String = '<map> \n<scene id="'+ mapId +'" name="'+ mapName +'" width="'+ TileLayer.MAP_LOGIC_WIDTH +'" height="'+ TileLayer.MAP_LOGIC_HEIGHT +'" pieceW="'+ Common.pieceWidth +'" pieceH="'+Common.pieceHeight+'" > \n';
				sceneData += '<gate kind="in" x="'+inGate.x+'" y="'+inGate.y+'" /> \n';
				sceneData += '<gate kind="out" x="'+outGate.x+'" y="'+outGate.y+'" /> \n';
				
				for(var i:int = 0; i < npcList.length; i ++)
				{
					if(npcList[i] is Npc)
					{
						var npc:Npc = npcList[i];
						sceneData += '<npc id="'+npc.npcId+'" x="'+npc.x+'" y="'+npc.y+'" /> \n';
					}
					
				}
				
				sceneData += "</scene> \n</map>";
				trace(sceneData);
				return sceneData;
				
			}
			return null;
		}
		
		public static function readTileBitmapdata(data:BitmapData):Array
		{
			var dataArray:Array = new Array();
			for(var i:int = 0; i < data.width; i ++)
			{
				dataArray[i] = new Array();
				for(var j:int = 0; j < data.height; j ++)
				{
					if(data.getPixel(i,j) == 0xFFFFFF)
						dataArray[i][j] = true;
					else
						dataArray[i][j] = false;
				}
			}
			
			return dataArray;
		}
		//
	}
}